package cn.ac.jaas.ljx.java.dynamicProxy;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;
import sun.misc.ProxyGenerator;  

public class DynamicProxy {
	
	public static void main(String[] args) {
		RealSubject real = new RealSubject();
		Subject proxySubject = (Subject) Proxy.newProxyInstance(Subject.class.getClassLoader(),
				new Class[] {Subject.class}, new ProxyHandler(real));
		
		proxySubject.doSomething();
		
		createProxyClassFile();
	}
	
	public static void createProxyClassFile() {
		String name = "ProxySubject";
		byte[] data = ProxyGenerator.generateProxyClass(name, new Class[] {Subject.class});
		try {
			FileOutputStream fos = new FileOutputStream(name + ".class");
			fos.write(data);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
